package com.example.mygame.controller;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteConstraintException;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mygame.R;
import com.example.mygame.service.Adopter;
import com.example.mygame.pojo.Friend;
import com.example.mygame.pojo.User;
import com.example.mygame.service.SqliteUtils;

import java.util.List;

public class AddFriend extends AppCompatActivity {

    private SqliteUtils sqliteUtils;

    private ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);

        listView = findViewById(R.id.list);

        sqliteUtils = new SqliteUtils(this);

        Intent getDataIntent = getIntent();

        String userName = getDataIntent.getStringExtra("userName");

        List<User> userList = sqliteUtils.queryAllFromDb();

        ListView listView = (ListView) findViewById(R.id.list);

        listView.setAdapter(new Adopter(userList, this));

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                builder.setTitle("添加好友");
                builder.setMessage("确定添加此好友?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        TextView name = view.findViewById(R.id.name);
                        String Fname = name.getText().toString();
                        TextView nick = view.findViewById(R.id.nick);
                        String Fnick = nick.getText().toString();
                        TextView sexAndAgeAndphone = view.findViewById(R.id.sexAndAgeAndphone);
                        String FSexAndAgeAndphone = sexAndAgeAndphone.getText().toString();
                        Friend friend = new Friend();
                        //用户名
                        friend.setUsername(userName);
                        friend.setFriendname(Fname);
                        friend.setNick(Fnick);
                        friend.setPhone(FSexAndAgeAndphone);
                        friend.setStatus(1);
                        try {
                            sqliteUtils.addFriend(friend);
                        } catch (Exception e) {
                            Toast.makeText(AddFriend.this, "不可重复添加好友!!!", Toast.LENGTH_LONG).show();
                        }
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(AddFriend.this, "取消", Toast.LENGTH_LONG).show();
                    }
                });
                builder.create().show();


            }
        });

    }
}